package com.example.administrator.yuejian23.Fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.yuejian23.Bean.ConInfo;
import com.example.administrator.yuejian23.Const;
import com.example.administrator.yuejian23.R;
import com.example.administrator.yuejian23.Retrofit.RetrofitService;
import com.example.administrator.yuejian23.adapter.ContestListAdapter;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class ContestFragment extends Fragment {
    private View view;
    private RecyclerView recyclerView;
    private List<ConInfo> list;
    private ContestListAdapter ConAdapter;
    private Context context;
    private Retrofit retrofit;
    public  ContestFragment() {
    }

        @Override
        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
            recyclerView = (RecyclerView) view.findViewById(R.id.Con);
            retrofit = new Retrofit.Builder().
                    baseUrl(Const.BASE_URL).
                    addConverterFactory(GsonConverterFactory.create()).
                    build();
            RetrofitService retrofitService = retrofit.create(RetrofitService.class);
            Call<List<ConInfo>> call = retrofitService.getConList();
            call.enqueue(new Callback<List<ConInfo>>() {
                @Override
                public void onResponse(Call<List<ConInfo>> call, Response<List<ConInfo>> response) {
                    if (getActivity() != null) {
                        list=response.body();
                        ConAdapter = new ContestListAdapter(getActivity(),list);
                        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
                        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                        recyclerView.setLayoutManager(linearLayoutManager);
                        recyclerView.setAdapter(ConAdapter);
                        recyclerView.setItemAnimator(new DefaultItemAnimator());

                    }

                }
                @Override
                public void onFailure(Call<List<ConInfo>> call, Throwable t) {
                    //  /*Toast.makeText(getActivity(),"获取列表失败",Toast.LENGTH_SHORT).show();*/
                }
            });

        }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_contest, container, false);
    }

}

